package org.micro.frame.multiple;

import org.micro.frame.multiple.dynamic.TargetDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

@RestController
public class MultipleController {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @RequestMapping("time")
    public Object time() {
        return new Date();
    }

    @RequestMapping("test")
    public List<Demo> get() {
        String sql = "select *from demo";
        return (List<Demo>) jdbcTemplate.query(sql, new RowMapper<Demo>() {
            @Override
            public Demo mapRow(ResultSet rs, int rowNum) throws SQLException {
                return new Demo(rs.getLong("id"), rs.getString("name"));
            }
        });
    }

    @TargetDataSource("ds1")
    @RequestMapping("test1")
    public List<Demo> get1() {
        String sql = "select *from demo1";
        return (List<Demo>) jdbcTemplate.query(sql, new RowMapper<Demo>() {
            @Override
            public Demo mapRow(ResultSet rs, int rowNum) throws SQLException {
                return new Demo(rs.getLong("id"), rs.getString("name"));
            }
        });
    }

    @TargetDataSource("ds2")
    @RequestMapping("test2")
    public List<Demo> get2() {
        String sql = "select *from demo2";
        return (List<Demo>) jdbcTemplate.query(sql, new RowMapper<Demo>() {
            @Override
            public Demo mapRow(ResultSet rs, int rowNum) throws SQLException {
                return new Demo(rs.getLong("id"), rs.getString("name"));
            }
        });
    }


    @TargetDataSource("ds3")
    @RequestMapping("test3")
    public List<Demo> get3() {
        String sql = "select *from demo3";
        return (List<Demo>) jdbcTemplate.query(sql, new RowMapper<Demo>() {
            @Override
            public Demo mapRow(ResultSet rs, int rowNum) throws SQLException {
                return new Demo(rs.getLong("id"), rs.getString("name"));
            }
        });
    }

}
